/*
 * ClassCommandParser.java
 *
 * Created on May 17, 2007, 6:01 PM
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.parsing;

/**
 * This class parses a String in the format of <modulename>{<moduleargs>} into separate values.
 *
 * @author Tal Eisenberg
 */
public class ModuleCommandParser {
    private String plugName, plugProps;
  
    /**
     * Creates an instance of ModuleCommandParser with the supplied properties
     */
    public ModuleCommandParser(String arg) throws IllegalArgumentException {
        //new format: MODULE{prop;prop;prop}
        arg = arg.trim();
        int openIdx = ParsingAid.indexOfUnnested(arg, '{', "([{'", ")]}'");

        if (openIdx==-1) {
            //no properties
            plugName = arg;
            plugProps = "";
        } else {
            int closeIdx = Tokenizer.balanced(arg, openIdx, '{', '}');
            if (closeIdx==-1)
                throw new IllegalArgumentException("Missing closing parenthesis '}':" + arg);
            if (arg.length()>closeIdx+1)
                throw new IllegalArgumentException("Command delimiter is missing ';' after module end: '" + arg + "'");
            plugName = arg.substring(0, openIdx).trim();
            plugProps = arg.substring(openIdx+1, closeIdx).trim();
        }
    }
    
    /**
     * Returns the Module name.
     */
    public String getModuleName() {
        return plugName;
    }
    
    /**
     * Returns the Module properties.
     */
    public String getModuleProperties() {
        return plugProps;
    }
}